import {createStore} from 'vuex'
import {login, selectAllTables, selectClass, selectClassState, selectPosition} from "../api";
import {setStaffInfo, setToken} from "../api/loginFunctions";
import router from "../router";
import { ElMessage } from 'element-plus'
import config from '../store/config/index.js'
// 创建一个新的 store 实例
const store = createStore({
    state() {
        return {
            count: 0,
            staffInfo: {},
            addPermission:false,
            positionList:[],
            classList:[],
            loading:false,
            allTablesList:[],
            tableId:"",
        }
    },
    mutations: {
        StartLoad(state){
            state.loading = true;
        },
        EndLoad(state){
            state.loading = false;
        },
        increment(state) {
            state.count++
        },
        LogIn(state, res) {
            setToken(res.identity);
            setStaffInfo(res.peopleInfo);
        },
        SetUserInfo(state,staffInfo){
            state.staffInfo = staffInfo;
        },
        SetAddPermission(state,addPermission){
            state.addPermission = addPermission;
        },
        SetPosition(state,positionList){
            state.positionList = positionList;
        },
        SetClass(state,classList){
            state.classList = classList;
        },
        SetAllTablesList(state,allTablesList){
            state.allTablesList = allTablesList;
        },
        SetAllTableId(state,tableId){
            state.tableId = tableId;
        }
    },
    actions: {
        async getAllTables({commit}){
            selectAllTables().then((res) => {
                if(res.code === 200){
                    commit('SetAllTablesList', res.tableList);
                }else{
                    console.log(res);
                }
            },(error)=>{
                console.log(error);
            });
        },
        async logIn({commit}, staffInfo) {
            login({username: staffInfo.userId, password: staffInfo.userPassword}).then((res) => {
                if (res.code === 200) {
                    router.replace({path:"/home"});
                    commit('LogIn', res);
                } else {
                    ElMessage({
                        showClose: true,
                        message: '登录失败,请检查工号或密码!',
                        type: 'warning',
                    });
                }
            });
        },
        async getAddPermission({commit}){
            return new Promise((resolve, reject) => {
                if(store.state.staffInfo.jurisdiction<=1){
                    console.log("获取添加权限了!!!!");
                    let data ={
                        classNum: store.state.staffInfo.userNum
                    }
                    selectClassState(data).then((res) => {
                        if(res.code === 200){
                            commit('SetAddPermission', res.state);
                            resolve(res.state);
                        }else{
                            resolve(false);
                            console.log(res);
                        }
                    },(error)=>{
                        console.log(error);
                        reject();
                    });
                }else {
                    resolve(true);
                    commit('SetAddPermission', true);
                }
            });
            //小于等于1是工班帐号需要获取权限
        },
        async getPosition({commit}) {
            selectPosition().then((res) => {
                if(res.code === 200){
                    commit('SetPosition',res.positionList);
                }else{
                    console.log(res);
                }
            },(error)=>{
                console.log(error);
            });
        },
        async getClass({commit}) {
            let data = {
                centerId: Number(store.state.staffInfo.centerId),
                jurisdiction: Number(store.state.staffInfo.jurisdiction)
            }
            selectClass(data).then((res) => {
                if(res.code === 200){
                    commit('SetClass',res.classList);
                }else{
                    console.log(res);
                }
            },(error)=>{
                console.log(error);
            });
        },
    },
    modules: {
        config,
    }
})

export default store
